﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
namespace XML
{
	public class ProductsDal
	{
		#region  BasicMethod

		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("product_id", "products"); 
		}

		public bool Exists(int product_id,string product_name)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from products");
			strSql.Append(" where product_id=@product_id and product_name=@product_name ");
			SqlParameter[] parameters = {
					new SqlParameter("@product_id", SqlDbType.Int,4),
					new SqlParameter("@product_name", SqlDbType.VarChar,50)			};
			parameters[0].Value = product_id;
			parameters[1].Value = product_name;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		public bool Add(Model.products model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into products(");
			strSql.Append("product_id,product_name)");
			strSql.Append(" values (");
			strSql.Append("@product_id,@product_name)");
			SqlParameter[] parameters = {
					new SqlParameter("@product_id", SqlDbType.Int,4),
					new SqlParameter("@product_name", SqlDbType.VarChar,50)};
			parameters[0].Value = model.product_id;
			parameters[1].Value = model.product_name;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	
		public bool Update(Model.products model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update products set ");
			strSql.Append("product_id=@product_id,");
			strSql.Append("product_name=@product_name");
			strSql.Append(" where product_id=@product_id and product_name=@product_name ");
			SqlParameter[] parameters = {
					new SqlParameter("@product_id", SqlDbType.Int,4),
					new SqlParameter("@product_name", SqlDbType.VarChar,50)};
			parameters[0].Value = model.product_id;
			parameters[1].Value = model.product_name;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		
		public bool Delete(int product_id,string product_name)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from products ");
			strSql.Append(" where product_id=@product_id and product_name=@product_name ");
			SqlParameter[] parameters = {
					new SqlParameter("@product_id", SqlDbType.Int,4),
					new SqlParameter("@product_name", SqlDbType.VarChar,50)			};
			parameters[0].Value = product_id;
			parameters[1].Value = product_name;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		public Model.products GetModel(int product_id,string product_name)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 product_id,product_name from products ");
			strSql.Append(" where product_id=@product_id and product_name=@product_name ");
			SqlParameter[] parameters = {
					new SqlParameter("@product_id", SqlDbType.Int,4),
					new SqlParameter("@product_name", SqlDbType.VarChar,50)			};
			parameters[0].Value = product_id;
			parameters[1].Value = product_name;

			Model.products model=new Model.products();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		public Model.products DataRowToModel(DataRow row)
		{
			Model.products model=new Model.products();
			if (row != null)
			{
				if(row["product_id"]!=null && row["product_id"].ToString()!="")
				{
					model.product_id=int.Parse(row["product_id"].ToString());
				}
				if(row["product_name"]!=null)
				{
					model.product_name=row["product_name"].ToString();
				}
			}
			return model;
		}

	
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select product_id,product_name ");
			strSql.Append(" FROM products ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

	
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" product_id,product_name ");
			strSql.Append(" FROM products ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

	
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM products ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.product_name desc");
			}
			strSql.Append(")AS Row, T.*  from products T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

        public List<Model.products> DataTableToList(DataTable dt)
        {
            List<Model.products> modelList = new List<Model.products>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.products model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }
		#endregion  BasicMethod
		
	}
}

